package com;

import lombok.SneakyThrows;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.tools.ant.taskdefs.Sleep;
import org.springframework.security.core.parameters.P;

import java.net.URI;
import java.sql.Blob;

/**
 * @author Tindy
 * @date 2022/1/24
 * @describe
 */
public class T {
    public static void main(String[] args) throws InterruptedException {
        new Thread(new T1("D:\\tmp\\td-data\\userhome\\wKgBc2IDFniAOJy6AAAB1r0Da40.keytab","hdfs/ztdata-0001","t1")).start();
        Thread.sleep(5000);
        new Thread(new T2("C:\\Users\\17006\\Downloads\\test2.keytab","test/test","t2")).start();
    }
}
class T1 implements Runnable{
    private String keytabPath;
    private String username;
    private String dn;
    public T1(String keytabPath,String username,String dn){
        this.username=username;
        this.keytabPath=keytabPath;
        this.dn=dn;
    }
    @SneakyThrows
    @Override
    public void run() {
        UserGroupInformation.setLoginUser(null);
        System.out.println("-----------1"+dn);
        String krb5ConfPath = "D:\\tmp\\td-data\\userhome\\wKgBc2IDFrSAYaviAAACJc574iQ144.ini";
        System.out.println(System.getProperty("java.security.krb5.conf"));
        System.setProperty("java.security.krb5.conf", krb5ConfPath);
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(username, keytabPath);
        System.clearProperty("java.security.krb5.conf");

        Blob b;

        FileSystem fs = FileSystem.get(new URI("hdfs://172.16.20.40:8020"), conf);
        System.out.println("-----------2"+dn);
        Thread.sleep(8000);
        System.out.println("-----------3"+dn);
        fs.mkdirs(new Path("/test/"+dn));
    }
}
class T2 implements Runnable{
    private String keytabPath;
    private String username;
    private String dn;
    public T2(String keytabPath,String username,String dn){
        this.username=username;
        this.keytabPath=keytabPath;
        this.dn=dn;
    }
    @SneakyThrows
    @Override
    public void run() {
        UserGroupInformation.setLoginUser(null);
        System.out.println("-----------1"+dn);
        String krb5ConfPath = "D:\\tmp\\td-data\\userhome\\wKgBc2IDFrSAYaviAAACJc574iQ144.ini";
        System.out.println(System.getProperty("java.security.krb5.conf"));
        System.setProperty("java.security.krb5.conf", krb5ConfPath);
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(username, keytabPath);
        System.clearProperty("java.security.krb5.conf");
        FileSystem fs = FileSystem.get(new URI("hdfs://172.16.20.40:8020"), conf);
        System.out.println("-----------2"+dn);
        Thread.sleep(8000);
        System.out.println("-----------3"+dn);
        fs.mkdirs(new Path("/test/"+dn));
    }
}
